/*
Copyright 2025 PerfXLab (Beijing) Technologies Co., Ltd.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

    http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#include "OpenSparseBLAS_func.h"
#include <complex>

// -----------------------------------------------------------------------------
//  OpenSpB_gather  —— 通用模板：x[i] = y[indx[i] * incy]
// -----------------------------------------------------------------------------
#include <complex>

template <typename T>
inline void OpenSpB_gather(
    int nz,
    const T* y, int incy,
    T* x, const int* indx,
    OpenSpB_base_type index_base)
{
    if (nz <= 0 || incy <= 0) return;
    const int base = (index_base == OPENSPBLAS_ONE_BASE) ? 1 : 0;
    for (int i = 0; i < nz; ++i) {
        const int idx = indx[i] - base;
        x[i] = y[idx * incy];
    }
}

// -----------------------------------------------------------------------------
// 单精度实数  sgather
// -----------------------------------------------------------------------------
void OpenSpB_sgather(
    int nz,
    const float* y, int incy,
    float* x, const int* indx,
    OpenSpB_base_type index_base)
{
    OpenSpB_gather<float>(nz, y, incy, x, indx, index_base);
}

// -----------------------------------------------------------------------------
// 双精度实数  dgather
// -----------------------------------------------------------------------------
void OpenSpB_dgather(
    int nz,
    const double* y, int incy,
    double* x, const int* indx,
    OpenSpB_base_type index_base)
{
    OpenSpB_gather<double>(nz, y, incy, x, indx, index_base);
}

// -----------------------------------------------------------------------------
// 单精度复数  cgather
// -----------------------------------------------------------------------------
void OpenSpB_cgather(
    int nz,
    const void* y, int incy,
    void* x, const int* indx,
    OpenSpB_base_type index_base)
{
    OpenSpB_gather<std::complex<float>>(
        nz,
        static_cast<const std::complex<float>*>(y),
        incy,
        static_cast<std::complex<float>*>(x),
        indx,
        index_base);
}

// -----------------------------------------------------------------------------
// 双精度复数  zgather
// -----------------------------------------------------------------------------
void OpenSpB_zgather(
    int nz,
    const void* y, int incy,
    void* x, const int* indx,
    OpenSpB_base_type index_base)
{
    OpenSpB_gather<std::complex<double>>(
        nz,
        static_cast<const std::complex<double>*>(y),
        incy,
        static_cast<std::complex<double>*>(x),
        indx,
        index_base);
}
